﻿/*
 * ========================================================================
 * Copyright(c) 2008-2010北京高阳金信信息技术有限公司, All Rights Reserved.
 * ========================================================================
 *  Jmail收发邮件类
 * 
 * 作者：周燕龙     时间：2010-06-18至21
 * 版本：V1.0
 * 
 * 修改者：         时间：                
 * 修改说明：
 * ========================================================================
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseMailUtil;
using jmail;


namespace JMailUitl
{
    public class JMail : Email
    {  
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="curMsg">邮件信息</param>
        /// <returns></returns>
        public override bool SendMail(MailInfo curMsg)
        {
            bool flag = false;
            MailInfo curInfo = new MailInfo();
            Message curmsg = new Message();
            //配置文件初始化MESSAGE成员变量

            curmsg.MailServerUserName = curMsg.ServerUsername;
            curmsg.MailServerPassWord = curMsg.ServerPassword;
            curmsg.From = curMsg.MailFrom;
            curmsg.Subject = curMsg.MailSubject;
            curmsg.Charset = curMsg.MailCharset;
            curmsg.Priority = curMsg.IPriority;
            curmsg.Body = curMsg.MailBody;
            curmsg.AddRecipient(curMsg.MailTo, "", "");
            curmsg.ISOEncodeHeaders = false;
          // curmsg.ContentType = "text/html";

            try { flag = curmsg.Send(curMsg.MailServer, false); }
            catch{ flag = false; } 
            return flag;
        }

        /// <summary>
        /// 接收邮件
        /// </summary>
        /// <param name="I_MailServer">服务器地址</param>
        /// <param name="I_UserName">服务器用户名</param>
        /// <param name="I_UserPwd">服务器密码</param>
        /// <param name="I_Port">端口号</param>
        /// <returns></returns>
        public override bool RecvMail(string I_MailServer, string I_UserName, string I_UserPwd, int I_Port)
        {
            bool flag = false;
            jmail.POP3Class mail = new POP3Class();
            jmail.Message msg = new jmail.MessageClass();

            try{mail.Connect(I_UserName, I_UserPwd, I_MailServer, I_Port);}
            catch {flag = false;}
            for (int i = 1; i <= mail.Count; i++)
            {
                msg = mail.Messages[i];
                //if (i == 1)
                //    mail.DeleteSingleMessage(i);               
                if (msg.HTMLBody != null)
                {
                    #region 数据处理
                    
                    #endregion
                }
            }
            flag = true;
            mail.Disconnect();
            return flag;
        }

        /// <summary>
        /// 接收邮件将邮件内容存储在Dictionary中
        /// </summary>
        /// <param name="I_MailServer">服务器地址</param>
        /// <param name="I_UserName">用户名</param>
        /// <param name="I_UserPwd">密码</param>
        /// <param name="I_Port">端口</param>
        /// <returns></returns>
        public override Dictionary<string, string> GetMail(string I_MailServer, string I_UserName, string I_UserPwd, int I_Port)
        {
            Dictionary<string, string> curD = new Dictionary<string, string>();
            jmail.POP3Class mail = new POP3Class();
            jmail.Message msg = new jmail.MessageClass();
            msg.Charset = "utf-8";

            try { mail.Connect(I_UserName, I_UserPwd, I_MailServer, I_Port); }
            catch { return curD; }
            for (int i = 1; i <= mail.Count; i++)
            {
                msg = mail.Messages[i];
                if (msg.Body != null)
                {
                    #region 数据处理
                    if (!curD.ContainsKey(msg.Subject))
                        curD.Add(msg.Subject, msg.Body);
                    #endregion
                }
            }
            mail.Disconnect();
            return curD;
        }

        /// <summary>
        /// 删除邮件
        /// </summary>
        /// <param name="I_MailServer"></param>
        /// <param name="I_UserName"></param>
        /// <param name="I_UserPwd"></param>
        /// <param name="I_Port"></param>
        public override void DelMail(string I_MailServer, string I_UserName, string I_UserPwd, int I_Port)
        {
            jmail.POP3Class mail = new POP3Class();
            try { mail.Connect(I_UserName, I_UserPwd, I_MailServer, I_Port); }
            catch { }
            mail.DeleteMessages();
            mail.Disconnect();
        }
    }
}
